//
//  BaseRefreshTableViewController.m
//  ZhiyunV3
//
//  Created by Leon on 16/7/29.
//  Copyright © 2016年 上海指旺信息科技有限公司. All rights reserved.
//

#import "BaseRefreshTableViewController.h"
#import "MJRefresh.h"
#import "XTNetReloader.h"
#import "WeChatStylePlaceHolder.h"

@interface BaseRefreshTableViewController() <CYLTableViewPlaceHolderDelegate, WeChatStylePlaceHolderDelegate>

@property (nonatomic, assign, getter=isNoResult) BOOL noResult;
@property (nonatomic, assign, getter=isNoNetwork) BOOL noNetwork;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;
@property (nonatomic, strong) MJRefreshAutoNormalFooter *nilFooter;

@end

@implementation BaseRefreshTableViewController

#pragma mark - Property
- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (BOOL)isNoResult {
    _noResult = (self.dataSource.count == 0);
    return _noResult;
}

-(void)viewDidLayoutSubviews {
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNumber = 1;
    self.pageSize = 50;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(beginToRequestData)];
//    // 马上进入刷新状态
//    [header beginRefreshing];
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(beginToRequestData)];
    self.footer = footer;
    // 禁止自动加载
    //    footer.automaticallyRefresh = NO;
    // 设置header
//    self.tableView.mj_header = header;
    self.pageNumber = 1;
    [self requestData];
}

#pragma mark - UITableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

#pragma mark - publice method
- (void)loadData:(NSArray *)dataArray {
    self.noNetwork = NO;
    if (self.tableView.mj_header.isRefreshing) {
        [self refreshNewData:dataArray];
    } else {
        [self appendMoreData:dataArray];
    }
    
}

- (void)appendMoreData:(NSArray *)dataArray {
    [self.dataSource addObjectsFromArray:dataArray];
    [self.tableView cyl_reloadData];
    [self.tableView.mj_footer endRefreshing];
    [self setTableViewFooter:dataArray.count];

}

- (void)refreshNewData:(NSArray *)dataArray {
    [self.dataSource removeAllObjects];
    [self.dataSource addObjectsFromArray:dataArray];
    [self.tableView cyl_reloadData];
    [self setTableViewFooter:dataArray.count];
    [self.tableView.mj_header endRefreshing];
}

- (void)setTableViewFooter:(NSInteger)count {
    if (count < self.pageSize) {
        self.tableView.mj_footer = self.nilFooter;
    } else {
        self.tableView.mj_footer = self.footer;
    }
}

- (void)networkError {
    self.noNetwork = YES;
    [self.dataSource removeAllObjects];
    [self.tableView cyl_reloadData];
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
    [self setTableViewFooter:0];
}

#pragma mark - override
- (void)beginToRequestData {
    if (self.tableView.mj_header.isRefreshing) {
        self.pageNumber = 1;
    } else {
        self.pageNumber++;
    }
    [self requestData];
}

- (void)requestData {
    
}

#pragma mark - CYLTableViewPlaceHolderDelegate Method

//- (UIView *)makePlaceHolderView {
//    if (self.isNoNetwork) {
//        return [self taoBaoStylePlaceHolder];
//    } else {
//        return [self weChatStylePlaceHolder];
//    }
//}
//
//- (UIView *)taoBaoStylePlaceHolder {
//    __block XTNetReloader *netReloader = [[XTNetReloader alloc] initWithFrame:CGRectMake(0, 0, 0, 0)
//                                                                  reloadBlock:^{
//                                                                      [self.tableView.mj_header beginRefreshing];
//                                                                  }] ;
//    return netReloader;
//}
//
//- (UIView *)weChatStylePlaceHolder {
//    WeChatStylePlaceHolder *weChatStylePlaceHolder = [[WeChatStylePlaceHolder alloc] initWithFrame:self.tableView.frame];
//    weChatStylePlaceHolder.delegate = self;
//    return weChatStylePlaceHolder;
//}

#pragma mark - WeChatStylePlaceHolderDelegate Method

- (void)emptyOverlayClicked:(id)sender {
    [self.tableView.mj_header beginRefreshing];
}

@end
